package com.xcity.game.task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.game.buff.Buff;
import com.xcity.game.buff.BuffEffector;
import com.xcity.game.buff.BuffTrigger;
import com.xcity.game.common.Cause;
import com.xcity.game.io.SerializeStream;
import com.xcity.game.io.Serializer;
import com.xcity.game.io.SerializerUtils;
import com.xcity.game.task.Task.State;

import naga.x.App;
import naga.x.game.time.TimeUpdater;

public class TaskManager implements BuffTrigger, Serializer {
	
	public static int CURRENT_VERSION = 3;
	
	@JSONField(serialize = false)
	private Player owner;
	private int version = 2;
	private List<Task> tasks;
	//上一次领取(完成当天所有每日任务)的奖励的日子
	private int getDay = 0;
	
	public TaskManager() {
		
	}
	
	public TaskManager(Player player) {
		this.owner = player;
		TaskService service = App.getApp().getServiceManager().get(TaskService.class);
		tasks = new ArrayList<Task>(2);
		tasks.add(new Task(this, service.getTemplate(Task.TYPE_MAIN, 0)));
//		tasks.add(new Task(this, service.getTemplate(Task.TYPE_DAILY, 0)));
	}
	
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public int getGetDay() {
		return getDay;
	}

	public void setGetDay(int getDay) {
		this.getDay = getDay;
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
	
	protected void onArchive(byte detectorId, Object... params) {
		TaskDetector detector = TaskDetectorFactory.create(detectorId);
		if (detector != null) {
			for (int i = 0; i < tasks.size(); i++) {
				Task task = tasks.get(i);
				if (detectorId != task.getTemplate().getConditionId()) {
					continue;
				}
				detector.onArchive(task, params);
				if (task.getState() == State.INIT
						&& task.getTemplate().getConditionId() == detectorId
						&& detector.isFinish(task)) {
					task.finish(Cause.AUTO);
				}
			}
		}
	}

	@Override
	public boolean trigger(Player player) {
		Buff buff = player.getBuffs().getBuffByEffect(BuffEffector.BUFF_AUTO_COMPLETE_DAILY_TASK);
		if (buff != null) {
			return buff.effect(player);
		}
		return false;
	}
	
	public void init(Player player) {
		this.owner = player;
		for (int i = 0; i < tasks.size(); i++) {
			Task t = tasks.get(i);
			t.setVm(this);
		}
	}

	public Player getOwner() {
		return owner;
	}
	
	public void setOwner(Player owner) {
		this.owner = owner;
	}
	
	public Task getTask(byte type) {
		for (int i = 0; i < tasks.size(); i++) {
			Task t = tasks.get(i);
			if (t.getTemplate().getType() == type) {
				return t;
			}
		}
		return null;
	}

	public void validate() {
		// 刷新日常任务
		TaskService service = App.getApp().getServiceManager().get(TaskService.class);
		if(version != CURRENT_VERSION){
			int size = service.getTemplateSize(Task.TYPE_DAILY);
			for(int i = tasks.size() - 1 ; i > -1; i--){
				if(tasks.get(i).getTemplate().getType() == Task.TYPE_DAILY){
					tasks.remove(i);
				}
			}
			for(int i = 0; i < size; i++){
				TaskTemplate temp = service.getTemplate(Task.TYPE_DAILY, i);
				Task task = new Task(this, temp);
				tasks.add(task);
			}
			version = CURRENT_VERSION;
		}else{
			Calendar cal = Calendar.getInstance();
			for(int i = 0 ; i < tasks.size(); i++){
				Task task = tasks.get(i);
				if(task.getTemplate().getType() == Task.TYPE_MAIN){
					continue;
				}
				cal.setTimeInMillis(task.getCreateTime());
				int day = (cal.get(Calendar.YEAR) << 16) | cal.get(Calendar.DAY_OF_YEAR);
				if(day != TimeUpdater.getInstance().today()){
					task.resetDayTask();
				}
			}
		}
		
		
//		Task task = getTask(Task.TYPE_DAILY);
//		Calendar cal = Calendar.getInstance();
//		cal.setTimeInMillis(task.getCreateTime());
//		int day = (cal.get(Calendar.YEAR) << 16) | cal.get(Calendar.DAY_OF_YEAR);
//		if (day != TimeUpdater.getInstance().today()) {
//			TaskTemplate first = service.getTemplate(Task.TYPE_DAILY, 0);
//			task.reinit(first);
//		}
	}
	
	public boolean finish(byte type, String cause) {
		boolean succ = false;
		for (int i = 0; i < tasks.size(); i++) {
			Task task = tasks.get(i);
			if (task.getTemplate().getType() == type && task.getState() == State.INIT) {
				task.finish(cause);
				succ = true;
			}
		}
		return succ;
	}
	
	public Task getTaskById(int tid){
		for(int i = 0; i < tasks.size(); i++) {
			Task task = tasks.get(i);
			if(task.getId() == tid){
				return task;
			}
		}
		return null;
	}

	@Override
	public void write(SerializeStream out) {
		SerializerUtils.serialize(this, out);
	}

}
